﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web
{
    public partial class index : SecurityPage
    {
        public int i = 0;
        public string GetXN()
        {
            i += 1;
            return i.ToString();
        }

        private void RepDataBing()
        {
            string XYDM = Session["SelectedXYDM"].ToString();
            List<StudentManage.Data.News> newslist = LinqUtility.LinqContext.News.Where(p => p.XYDM == "0" || p.XYDM == XYDM).OrderByDescending(p => p.AddTime).Take(4).ToList();
            this.RepNewsList.DataSource=newslist;
            this.RepNewsList.DataBind();
        }

        private void RepNoticeBind()
        {
            string XYDM = Session["SelectedXYDM"].ToString();
            List<Notice> noticelist = LinqUtility.LinqContext.Notice.Where(p => p.XYDM == "0" || p.XYDM == XYDM).OrderByDescending(p => p.AddTime).Take(8).ToList();
            this.RepNoticeList.DataSource = noticelist;
            this.RepNoticeList.DataBind();
        }

        private void RepPostBind()
        {
            string XYDM = Session["SelectedXYDM"].ToString();

            List<Project> Projectlist = Project.Where("ProjectTypeid=@0 and IsAudit=@1 and IsDel=@2 and College=@3", Convert.ToInt32(EnumData.ProjectType.勤工助学), true, false, XYDM).OrderByDescending(p => p.AddTime).Take(6).ToList();
            this.RepPostList.DataSource = Projectlist;
            this.RepPostList.DataBind();

            List<Project> GYHD = Project.Where("ProjectTypeid=@0 and IsAudit=@1 and IsDel=@2 and College=@3", Convert.ToInt32(EnumData.ProjectType.公益活动), true, false, XYDM).OrderByDescending(p => p.AddTime).Take(6).ToList();
            this.RepGYHDList.DataSource = GYHD;
            this.RepGYHDList.DataBind();

            List<Project> ZYHD = Project.Where("ProjectTypeid=@0 and IsAudit=@1 and IsDel=@2 and College=@3", Convert.ToInt32(EnumData.ProjectType.志愿活动), true, false, XYDM).OrderByDescending(p => p.AddTime).Take(6).ToList();
            this.RepZYHDList.DataSource = ZYHD;
            this.RepZYHDList.DataBind();

            List<Project> SHSJ = Project.Where("ProjectTypeid=@0 and IsAudit=@1 and IsDel=@2 and College=@3", Convert.ToInt32(EnumData.ProjectType.社会实践), true, false, XYDM).OrderByDescending(p => p.AddTime).Take(6).ToList();
            this.RepSHSJList.DataSource = SHSJ;
            this.RepSHSJList.DataBind();

            var q = from A in LinqUtility.LinqContext.Project
                    join B in LinqUtility.LinqContext.Apply
                    on A.id equals B.Projectid
                    where A.IsDel == false && A.IsAudit == true && (A.College == "0" || A.College == XYDM)
                    group A by new { A.id, A.Typeid, A.Title } into g
                    orderby g.Count() descending
                    select new
                    {
                        g.Key.id,
                        g.Key.Typeid,
                        g.Key.Title,
                        SumCount = g.Count()
                    };

            this.RepHotPostList.DataSource = q.ToList();
            this.RepHotPostList.DataBind();
        }

        public string GetTypeName(string typeid)
        {
            TypeInfo md=TypeInfo.Where("id=@0", Convert.ToInt32(typeid)).FirstOrDefault();
            return md == null ? "" : md.TypeName;
        }

        private void RepImgBind()
        {
            List<ImgList> list = ImgList.Where("IsVisible=@0", true).OrderByDescending(p => p.AddTime).ToList();
            this.Rep_imglist.DataSource = list;
            this.Rep_imglist.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            UC_MasterDrop uc = Page.Controls.AllOfType<UC_MasterDrop>().FirstOrDefault();
            uc.SelectedChanging += new UC_MasterDrop.DropEventDelegate(Drop_xy_SelectedChanging);

            if (IsPostBack) return;
            RepDataBing();
            RepNoticeBind();
            RepPostBind();
            RepImgBind();
        }

        private void Drop_xy_SelectedChanging()
        {
            RepDataBing();
            RepNoticeBind();
            RepPostBind();
        }
    }
}